<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar> </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.projectname }}
        </a>
      </template>
      <template #projectno="{ record }">
        <!-- <a @click="handleDetailsForm(record)" >
          <span style="text-decoration: underline;margin-right: 5px;">查看</span>
        </a>
        {{ record.projectno }} -->
        <a @click="handleDetailsForm(record)">
          {{ record.projectno }}
        </a>
      </template>

      <template #uploadFile1="{ record }">
        <BasicUpload
          v-if="uploadshow"
          v-model:value="record.dataMap"
          disabled
          :bizKey="record.id"
          :bizType="'PmWaterSupply_xc_image'"
          :uploadType="'all'"
          :loadTime="record.__t"
          :showPreview="true"
          :isPreview="false"
          :emptyHidePreview="true"
        />
      </template>

      <template #uploadFile2="{ record }">
        <BasicUpload
          v-if="uploadshow"
          v-model:value="record.dataMap"
          disabled
          :bizKey="record.id"
          :bizType="'PmWaterSupply_ys_image'"
          :uploadType="'all'"
          :loadTime="record.__t"
          :showPreview="true"
          :isPreview="false"
          :emptyHidePreview="true"
        />
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <InputFormMore @register="registerMoreDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsAcceptSupplyPmWatersupplyList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, onMounted, ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    pmWatersupplyDelete,
    pmWatersupplyListData,
    pmWatersupplyCancel,
    pmWatersupplySubmit,
  } from '/@/api/accept/supply/pmWatersupply';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import InputFormMore from './formMore.vue';
  import { BasicUpload } from '/@/components/Upload';
  import { useUserStore } from '/@/store/modules/user';
  const userStore = useUserStore();

  const { t } = useI18n('accept.supply.pmWatersupply');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('竣工验收管理'),
  };

  let uploadshow = ref<any>(true);

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('发起时间'),
        field: 'createDate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('项目编号'),
        field: 'projectno',
        component: 'Input',
      },
      {
        label: t('项目名称'),
        field: 'projectname',
        component: 'Input',
      },
      {
        label: t('发起人名称'),
        field: 'createByName',
        component: 'Input',
      },
      // {
      //   label: t('发起人编码'),
      //   field: 'createBy',
      //   component: 'Input',
      // },
      {
        label: t('验收结果'),
        field: 'checkresult',
        component: 'Select',
        componentProps: {
          dictType: 'zf_assay_result',
        },
      },
      {
        label: t('是否推送报装'),
        field: 'bpushed',
        component: 'Select',
        componentProps: {
          dictType: 'sys_yes_no',
        },
      },
      // {
      //   label: t('状态'),
      //   field: 'status',
      //   component: 'Select',
      //   componentProps: {
      //     dictType: 'zf_jgys_status',
      //     allowClear: true,
      //     onChange: handleSuccess,
      //   },
      // },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('项目编号'),
      dataIndex: 'projectno',
      key: 'a.projectno',
      sorter: true,
      width: 130,
      align: 'left',
      slot: 'projectno',
      fixed: 'left',
    },
    {
      title: t('项目名称'),
      dataIndex: 'projectname',
      key: 'a.projectname',
      sorter: true,
      width: 200,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('发起时间'),
      dataIndex: 'createDate',
      key: 'a.createtime',
      sorter: true,
      width: 100,
      align: 'left',
      // fixed:'left'
    },
    {
      title: t('验收时间'),
      dataIndex: 'checktime',
      key: 'a.checktime',
      sorter: true,
      width: 100,
      align: 'left',
      // fixed:'left'
    },
    {
      title: t('发起人名称'),
      dataIndex: 'createByName',
      key: 'a.sponsor',
      sorter: true,
      width: 100,
      align: 'left',
    },
    // {
    //   title: t('发起人编码'),
    //   dataIndex: 'createBy',
    //   key: 'a.sponsorno',
    //   sorter: true,
    //   width: 130,
    //   align: 'left',
    // },
    {
      title: t('验收结果'),
      dataIndex: 'checkresult',
      key: 'a.checkresult',
      sorter: true,
      width: 130,
      align: 'left',
    },
    // {
    //   title: t('不合格描述'),
    //   dataIndex: 'remarks',
    //   key: 'a.remarks',
    //   sorter: true,
    //   width: 200,
    //   align: 'left',
    // },
    {
      title: t('现场图片'),
      dataIndex: 'xianchang',
      width: 130,
      align: 'left',
      slot: 'uploadFile1',
    },
    {
      title: t('验收图片'),
      dataIndex: 'yanshou',
      width: 130,
      align: 'left',
      slot: 'uploadFile2',
    },
    {
      title: t('是否推送报装'),
      dataIndex: 'bpushed',
      key: 'a.bpushed',
      sorter: true,
      width: 80,
      align: 'center',
      dictType: 'sys_yes_no',
    },
    // {
    //   title: t('状态'),
    //   dataIndex: 'status',
    //   key: 'a.status',
    //   sorter: true,
    //   width: 130,
    //   align: 'center',
    //   dictType: 'zf_jgys_status',
    // },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        label: '补充资料',
        onClick: handleMoreForm.bind(this, { id: record.id }),
        // auth: 'btn:edit:jungong',
        // ifShow: record.status == 9,
      },
      // {
      //   icon: 'clarity:note-edit-line',
      //   title: t('编辑竣工验收'),
      //   onClick: handleForm.bind(this, { id: record.id }),
      //   auth: 'btn:edit:jungong',
      //   ifShow: record.status == 9,
      // },
      // {
      //   // icon: 'clarity:note-edit-line',
      //   title: t('撤销竣工'),
      //   label: '撤销竣工',
      //   onClick: cancleForm.bind(this, { id: record.id }),
      //   auth: 'btn:cancel:jungong',
      //   ifShow: record.bpushed != 1,
      // },
      // {
      //   // icon: 'clarity:note-edit-line',
      //   title: t('提交竣工'),
      //   label: '提交竣工',
      //   onClick: submitForm.bind(this, { record }),
      //   auth: 'btn:confirm:jungong',
      //   ifShow: record.status == 9,
      // },
      // {
      //   icon: 'ant-design:delete-outlined',
      //   color: 'error',
      //   title: t('删除竣工验收'),
      //   popConfirm: {
      //     title: t('是否确认删除竣工验收'),
      //     confirm: handleDelete.bind(this, { id: record.id }),
      //   },
      //   auth: 'btn:delete:jungong',
      //   ifShow: record.status == 9,
      // },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerMoreDrawer, { openDrawer: openMoreDrawer }] = useDrawer();

  const [registerTable, { reload, getForm }] = useTable({
    api: pmWatersupplyListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    immediate: false,
  });

  function handleDetailsForm(record: Recordable) {
    router.push({
      path: '/proj/info/pmProjectinformation/detailsForm',
      query: {
        id: record.projInfo.id,
        code: record.projectno,
        tabTitle: `项目详情(${record.projectno})`,
      },
    });
  }

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  function handleMoreForm(record: Recordable) {
    openMoreDrawer(true, record);
  }

  async function cancleForm(record: Recordable) {
    // openDrawer(true, record);
    const res = await pmWatersupplyCancel(record);
    showMessage(res.message);
    handleSuccess();
  }

  async function submitForm(record: Recordable) {
    // openDrawer(true, record);
    const res = await pmWatersupplySubmit(record);
    showMessage(res.message);
    handleSuccess();
  }

  async function handleDelete(record: Recordable) {
    const res = await pmWatersupplyDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
    uploadshow.value = false;
    setTimeout(() => {
      uploadshow.value = true;
    }, 800);
  }

  onMounted(async () => {
    handleProjecte();
  });
  async function handleProjecte() {
    await getForm().setFieldsValue({
      projectno: userStore.getProjecte.code,
    });
    reload();
    await getForm().setFieldsValue({
      projectno: userStore.getProjecte.code,
    });
  }
</script>
